<?php

/**
 * @author Sergey
 */
require_once 'MarcConstants.php';
class MarcSubfield {
    protected $code;
    protected $data;
    /**
     *
     * @param string $code code of subfield
     * @param string $data data of subfield
     */
    public function  __construct($code, $data)
    {
        $code = trim($code);
        $this->code = $code;
        $this->data = $data;
    }
    public function  __destruct() {
        $this->code = null;
        $this->data = null;
    }

    public function delete(){
        $this->__destruct();
    }
    /**
     *
     * @return string code of subfield
     */
    public function getCode()
    {
        return $this->code;
    }
    /**
     *
     * @return string data of subfield
     */
    public function getData()
    {
        return $this->data;
    }
    /**
     *
     * @return bool
     */
    public function isEmpty()
    {
        if ($this->data) {
            return false;
        }
        return true;
    }
    /**
     * @param string $toEncoding raw encoding. Default UTF-8
     * @return string raw string of subfield data
     */
    public function toRaw($toEncoding = null)
    {
        if ($toEncoding !== null){
            return MarcConstants::SUBFIELD_INDICATOR.$this->code.mb_convert_encoding($this->data, $toEncoding,'UTF-8');
        }
        return MarcConstants::SUBFIELD_INDICATOR.$this->code.$this->data;
    }
    public function  __toString($toEncoding = null) {
        if ($toEncoding !== null){
            return mb_convert_encoding($this->data, $toEncoding,'UTF-8');
        }
        return $this->data;
    }
}
?>
